package com.walker.controller.catalogue;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.walker.anno.ResponseResult;
import com.walker.service.catalogue.CatalogueHomeService;
import com.walker.service.meta.AssetCatalogueFullSearchServ;

import net.sf.json.JSONObject;

/**
 * 资产-目录-首页
 */
@RestController
@RequestMapping(value = "/catalogueHome")
@ResponseResult
public class CatalogueHomeController {

	private static final Log log = LogFactory.getLog(CatalogueHomeController.class);
	
	@Resource
	private CatalogueHomeService service;
	
	@Autowired
	private AssetCatalogueFullSearchServ search;
	
	/**
	 * 全局搜索
	 * @param keyword
	 * @return
	 */
	@PostMapping("getGobalSerachList")
	public Map<String, Object> getGobalSerachList(@RequestBody(required=false) String keyword) {
		return service.getGobalSerachList(keyword);
	}
	
	/**
	 *数据板块信息 
	 * @return
	 */
	@PostMapping("listBusiness")
	public List<Map<String,Object>> listBusiness() {
		return service.listBusiness();
	}
	
	/**
	 * 数据域信息
	 * @param busin_id
	 * @return
	 */
	@PostMapping("getDataFieldByBusinId")
	public List<Map<String,Object>> getDataFieldByBusinId(@RequestBody String busin_id) {
		return service.getDataFieldByBusinId(busin_id);
	}
	
	/**
	 * 欢迎信息
	 * @param login_code
	 * @return
	 */
	@PostMapping("getWelcomeInfo")
	public String getWelcomeInfo(@RequestBody(required = false) String login_code) {
		return service.getWelcomeInfo(login_code);
	}
	
	/**
	 * 最近浏览
	 * @param login_code
	 * @return
	 */
	@PostMapping("listTrail")
	public List<Map<String, Object>> listTrail(@RequestHeader(value = "login_code") String login_code) {
		return service.listTrail(login_code);
	}
	
	/**
	 * 最近收藏 
	 * @param login_code
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@PostMapping("listCollect")
	@ResponseResult(msg="doNotOperateReturnResult")
	public String listCollect(@RequestHeader(value = "login_code") String login_code,@RequestBody String request) {
		Map<String, Object> params = JSONObject.fromObject(request);
		JSONObject json = new JSONObject();
		try{
			Map<String,Object> data = service.listCollect(params,login_code);
			json.put("data", data.get("data"));
			json.put("totalRecords", data.get("totalRecords"));
			json.put("flag",true);
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag", false);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}
	
	/**
	 * 最近使用
	 * @param login_code
	 * @return
	 */
	@PostMapping("listHot")
	public List<Map<String, Object>> listHot(@RequestHeader(value = "login_code") String login_code) {
		return service.listHot(login_code);
	}
	
	/**
	 * 全文检索
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@PostMapping("/fullsearch")
	public List<Object> fullsearch(@RequestBody(required=false) String request) throws Exception {
		Map<String, Object> params = JSONObject.fromObject(request);
		String keyword = String.valueOf(params.get("keyword"));
		return search.getIndexDocument(keyword);
	}
}